﻿using Main.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Main.Services
{
    public class SalaryService : ISalaryService
    {


        /*
         * 增加
         * 修改
         * 删除
         * 查询
         */
        public async Task<UIMessageCarry> AddAsync(SalaryItem item)
        {
            UIMessageCarry uIMessageCarry = new UIMessageCarry();
            using (SalaryContext context = new SalaryContext())
            {
                //先要判断存不存在
                if (context.SalaryItems.Any(s => s.MonthIndex == item.MonthIndex && s.Name == item.Name))
                {
                    //存在
                    uIMessageCarry.Message = $"“{item.Name}” 已存在，更新数据";
                    var oldItem = context.SalaryItems.Single(s => s.MonthIndex == item.MonthIndex && s.Name == item.Name);
                    oldItem.Effective = true;
                    oldItem.Value = item.Value;
                    uIMessageCarry.Statu = await context.SaveChangesAsync() > 0 ? true : false;
                    return uIMessageCarry;
                }
                context.SalaryItems.Add(item);
                uIMessageCarry.Statu = await context.SaveChangesAsync() > 0 ? true : false;
                return uIMessageCarry;

            }
          

        }
        public async Task<UIMessageCarry> AddAsync(List<SalaryItem> items)
        {
            UIMessageCarry uIMessageCarry = new UIMessageCarry();
            int addNew = 0;
            int modify=0;
            using (SalaryContext context = new SalaryContext())
            {
                //先要判断存不存在
                items.ForEach(item=> {

                    if (context.SalaryItems.Any(s => s.MonthIndex == item.MonthIndex && s.Name == item.Name))
                    {
                        //存在
                        // uIMessageCarry.Message = $"“{item.Name}” 已存在，更新数据";
                         modify++;
                        var oldItem = context.SalaryItems.Single(s => s.MonthIndex == item.MonthIndex && s.Name == item.Name);
                        oldItem.Effective = true;
                        oldItem.Value = item.Value;
                    }
                    else
                    {
                        addNew++;
                        context.SalaryItems.Add(item);
                    }

                });
                uIMessageCarry.Message = $"新增{addNew}个，更新{modify}个";
                uIMessageCarry.Statu = await context.SaveChangesAsync() > 0 ? true : false;
                return uIMessageCarry;
            }

        }

        public async Task<UIMessageCarry> ModifyAsync(SalaryItem item)
        {
            UIMessageCarry uIMessageCarry = new UIMessageCarry();
            using (SalaryContext context = new SalaryContext())
            {
               var oldVale= context.SalaryItems.Single(s=>s.SalaryItemId==item.SalaryItemId);

                oldVale.Name = item.Name;
                oldVale.Value = item.Value;
                uIMessageCarry.Statu = await context.SaveChangesAsync() > 0 ? true : false;
            }
            return uIMessageCarry;
        }

        public async Task<List<SalaryItem>> QueryAsync(int StartIndex, int EndIndex)
        {
            UIMessageCarry uIMessageCarry = new UIMessageCarry();
            using (SalaryContext context = new SalaryContext())
            {
                var res = context.SalaryItems.Where(s=>s.MonthIndex>=StartIndex&&s.SalaryItemId<=EndIndex);
                return await res.ToListAsync();
            }

        }

        public async Task<bool> RemoveAsync(int itemID)
        {
            using (SalaryContext context = new SalaryContext())
            {
                var oldVale = context.SalaryItems.Single(s => s.SalaryItemId == itemID);

                oldVale.Effective = false;

                return await context.SaveChangesAsync() > 0 ? true : false;
            }
        }
    }
}
